﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI;
using YArchitech.HCLoad.Properties;

namespace YArchitech.HCLoad.Export
{
	public class ExportGBXML
	{
		public ExternalCommandData CommandData
		{
			get
			{
				return this.m_commandData;
			}
		}

		public Document ActiveDocument
		{
			get
			{
				return this.m_activeDoc;
			}
		}

		public string ExportFileName
		{
			get
			{
				return this.m_exportFileName;
			}
			set
			{
				this.m_exportFileName = value;
			}
		}

		public string ExportFolder
		{
			get
			{
				return this.m_exportFolder;
			}
			set
			{
				this.m_exportFolder = value;
			}
		}

		public string ActiveDocName
		{
			get
			{
				return this.m_activeDocName;
			}
			set
			{
				this.m_activeDocName = value;
			}
		}

		public string ActiveViewName
		{
			get
			{
				return this.m_activeViewName;
			}
			set
			{
				this.m_activeViewName = value;
			}
		}

		public bool Is3DView
		{
			get
			{
				return this.m_is3DView;
			}
			set
			{
				this.m_is3DView = value;
			}
		}

		public ExportGBXML(ExternalCommandData commandData)
		{
			this.hwnd = Process.GetCurrentProcess().MainWindowHandle;
			this.m_commandData = commandData;
			this.m_activeDoc = commandData.Application.ActiveUIDocument.Document;
			this.CommandData.Application.Application.FileExported += new EventHandler<FileExportedEventArgs>(this.OnExported);
		}

		[DllImport("USER32.DLL")]
		private static extern bool PostMessage(IntPtr hWnd, uint msg, uint wParam, uint lParam);

		[DllImport("user32.dll")]
		public static extern int SendMessage(IntPtr hWnd, uint msg, uint wParam, uint lParam);

		public void RunGBXMLMenuFunction()
		{
			ExportGBXML.PostMessage(this.hwnd, this.WM_SYSKEYDOWN, 70u, this.VK_ALT);
			ExportGBXML.PostMessage(this.hwnd, this.WM_SYSKEYDOWN, 84u, 0u);
			ExportGBXML.PostMessage(this.hwnd, this.WM_SYSKEYDOWN, 71u, 0u);
		}

		public void OnExported(object obj, EventArgs args)
		{
			if (ExportGBXML.bYArchitechProc)
			{
				MessageBox.Show(Resources.MSG_Export_Succeed, Resources.MSGBOX_TITLE);
			}
			ExportGBXML.bYArchitechProc = false;
		}

		public virtual bool Export()
		{
			ExportGBXML.bYArchitechProc = true;
			this.RunGBXMLMenuFunction();
			return true;
		}

		private IntPtr hwnd;

		protected ExternalCommandData m_commandData;

		protected Document m_activeDoc;

		protected string m_exportFileName;

		protected string m_exportFolder;

		protected string m_activeDocName;

		protected string m_activeViewName;

		protected bool m_is3DView;

		protected string m_title;

		private static bool bYArchitechProc;

		private uint WM_SYSKEYDOWN = 260u;

		private uint VK_ALT = 536870912u;
	}
}
